package com.study.demoservice.controller;

import com.atguigu.commonutils.R;
import com.study.demoservice.bean.dto.PersonRequest;
import com.study.demoservice.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;

/**
 * @author chenyeqing
 */
@RestController
@RequestMapping("/api/persons")
@Validated
public class ValidController {

    @Autowired
    private PersonService personService;

    @PostMapping
    public R save(@RequestBody @Valid PersonRequest personRequest) {
        return R.ok().data("data", personRequest);
    }

    @PostMapping("/service")
    public R saveByService(@RequestBody PersonRequest personRequest) {
        personService.validatePersonRequest(personRequest);
        return R.ok().data("data", personRequest);
    }

    @GetMapping("/{id}")
    public R getPersonById(@PathVariable("id") @Valid @Max(value = 5, message = "超过 id 的范围了") Integer id) {
        return R.ok().data("id", id);
    }

    @GetMapping
    public R getPersonByName(@RequestParam("name") @Valid @Size(max = 6, message = "超过 name 的范围了") String name) {
        return R.ok().data("name", name);
    }



}
